/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.persistence.entity.failover;

import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Entity to hold the parameter data of failed processes.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 22:42 21.09.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
@Entity
@Table(name="FAILUREOBJECT")
public class FailureObject {

    /** The id field. */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    /** Document identifier to find and access it outside the system. */
    @Column(nullable = false, length = 250)
    private String className;

    /** Created timestamp. */
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;

    /** Created timestamp. */
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastRedo;

    /** The serialized parameter data. */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private byte[] serializedData;

    /** Initializes the Dates. */
    @PrePersist
    private void updateDates() {
        Calendar calendar = Calendar.getInstance();
        if (created == null) {
            created = new Date(calendar.getTimeInMillis());
        } else {
            lastRedo = new Date(calendar.getTimeInMillis());
        }
    }


    /**
     * Returns the id.
     *
     * @return the id.
     */
    public int getId() {
        return id;
    }

    /**
     * Sets the id.
     *
     * @param id the id.
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Returns the parameters class name.
     *
     * @return the class name.
     */
    public String getClassName() {
        return className;
    }

    /**
     * Sets the parameters class name.
     *
     * @param className the class name.
     */
    public void setClassName(String className) {
        this.className = className;
    }

    /**
     * Returns the serialized data.
     *
     * @return the serialized data.
     */
    public byte[] getSerializedData() {
        return serializedData;
    }

    /**
     * Sets the serialized data.
     *
     * @param serializedData the serialized data.
     */
    public void setSerializedData(byte[] serializedData) {
        this.serializedData = serializedData;
    }


    /**
     * Gets the lastRedo.
     *
     * @return The lastRedo value.
     */
    public Date getLastRedo() {
        return lastRedo;
    }

    /**
     * Gets the created.
     *
     * @return The created value.
     */
    public Date getCreated() {
        return created;
    }

    /**
     * Sets the lastRedo.
     *
     * @param lastRedo The lastRedo value to set.
     */
    public void setLastRedo(Date lastRedo) {
        this.lastRedo = lastRedo;
    }

    /**
     * Sets the created.
     *
     * @param created The created value to set.
     */
    public void setCreated(Date created) {
        this.created = created;
    }
}
